#!/bin/sh

[ -e "/etc/openvpn.user" ] && {
    env -i ACTION="$ACTION" INSTANCE="$INSTANCE" \
        /bin/sh \
        /etc/openvpn.user \
        $*
}

# Wrap user defined scripts on up/down/route-up/route-pre-down/ipchange events
# Scripts set with up/down/route-up/route-pre-down/ipchange in the openvpn config are also executed with the command=user_xxxx
case "$ACTION" in
    up)
        command=$user_up
        # Execute update-resolv-conf script after the VPN connection is established
        sh /etc/sysp/update-resolv-conf 
        ;;
    down)
        command=$user_down
        # Execute update-resolv-conf script after the VPN connection is terminated
        sh /etc/sysp/update-resolv-conf-openvpn down
        ;;
    route-up)
        command=$user_route_up
        ;;
    route-pre-down)
        command=$user_route_pre_down
        ;;
    ipchange)
        command=$user_ipchange
        ;;
    *)
        command=
        ;;
esac

if [ -n "$command" ]; then
    shift
    exec /bin/sh -c "$command $*"
fi

exit 0
